﻿using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageDevicetable.Command
{
    public class EditManageDeviceCommand : IRequest<Json<string>>
    {
        public int id { get; set; }
        /// <summary>
        /// 设备编码
        /// </summary>
        public string device { get; set; }
        /// <summary>
        /// 客户账号
        /// </summary>
        public string useraccount { get; set; }
    }

    public class EditManageDeviceCommandHandler : IRequestHandler<EditManageDeviceCommand, Json<string>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IUsertableRepository _iusertableRepository;
        private readonly IDevicetableRepository _ideviceRepository;
        public EditManageDeviceCommandHandler(ICurrentUserService icurrentUserServices, IUsertableRepository iusertableRepository, IDevicetableRepository ideviceRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iusertableRepository = iusertableRepository;
            _ideviceRepository = ideviceRepository;
        }
        public async Task<Json<string>> Handle(EditManageDeviceCommand request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(request.device))
            {
                return Json<string>.Failed("设备编码不能为空", "");
            }
            else if (request.id <= 0)
            {
                return Json<string>.Failed("请选择需要修改的设备", "");
            }
            //else if ((await _ideviceRepository.GetDeviceCount(request.id, request.device, cancellationToken)) > 0)
            //{
            //    return Json<string>.Failed("当前设备编码已经存在，请修改", "");
            //}
            else
            {
                int userid = _icurrentUserServices.GetUserId();


                devicetable model = await _ideviceRepository.GetDevicetableByid(request.id, cancellationToken);
                if (!(model != null && model.id > 0))
                {
                    return Json<string>.Failed("请选择有效的设备进行修改", "");
                }
                if (model.userid != userid)
                {
                    return Json<string>.Failed("请联系管理员修改设备", "");
                }
                else
                {
                    int bindUserid = 0;
                    if (!string.IsNullOrWhiteSpace(request.useraccount))
                    {
                        bindUserid = await _iusertableRepository.GetUserTableIdByAccount(userid, request.useraccount, cancellationToken);
                        if (bindUserid <= 0)
                        {
                            return Json<string>.Failed("请输入正确的客户名", "");
                        }
                    }
                    else if (request.device != model.deviceid)
                    {
                        return Json<string>.Failed("设备编码不允许修改", "");
                    }
                    model.deviceid = request.device;
                    model.binduserid = bindUserid;
                    model.updatetime = DateTime.Now;
                    await _ideviceRepository.EditManageDeviceTable(model, cancellationToken);
                    return Json<string>.Success("修改成功", "");
                }
            }
        }
    }
}
